<script setup lang="ts">
import type { ArticleParams } from '@/types/article'
import { ElNotification, ElMessageBox } from 'element-plus'
import { ref, onMounted } from 'vue'
import { checkArticle, getArticleInfo, delArticle } from '@/api/article'
import { formatDateFull } from '@/utils/format'

// 获取视频数据
const articleData = ref<ArticleParams[]>([])
const loading = ref(false)
// 分页
const total = ref(0)
const params = ref({
  // 当前页
  page: 1,
  // 当前生效的每页条数
  pageSize: 10,
  // 审核状态
  status: '',
  // 关键字
  key: ''
})
// 条件查询
const options = [
  {
    value: 0,
    label: '待审核'
  },
  {
    value: 1,
    label: '审核通过'
  },
  {
    value: 2,
    label: '审核未通过'
  }
]
const getArticle = async () => {
  loading.value = true
  const result = await getArticleInfo(params)
  articleData.value = result.data
  // @ts-ignore
  total.value = result.total
  articleData.value.map(
    (item: ArticleParams) =>
      (item.create_time = formatDateFull(item.create_time))
  )
  articleData.value = articleData.value.filter((item) => item.status !== 3)
  loading.value = false
}
const handleSizeChange = (size: number) => {
  params.value.page = 1
  params.value.pageSize = size
  getArticle()
}
const handleCurrentChange = (value: number) => {
  params.value.page = value
  getArticle()
}
const searchBtn = () => {
  getArticle()
}
const resetBtn = () => {
  params.value.status = ''
  params.value.key = ''
  getArticle()
}

// 是否显示抽屉
const articleDetail = ref<ArticleParams>({} as ArticleParams)
const isShow = ref(false)
const selectDetail = (row: ArticleParams) => {
  isShow.value = true
  articleDetail.value = row
}
// 是否显示图片预览器
const isShowImg = ref(false)
const currentIndex = ref(-1)
const showImgViewer = (index: number) => {
  isShowImg.value = true
  currentIndex.value = index
}
// 文章审核
const btnOK = async (flag: number) => {
  articleDetail.value.status = flag
  await checkArticle(articleDetail.value)
  isShow.value = false
  ElNotification({
    title: '温馨提示',
    message: '审核完成',
    type: 'success',
    duration: 1500
  })
  getArticle()
}
const btnCheckClose = async (row: ArticleParams, flag: number) => {
  articleDetail.value = row
  btnOK(flag)
}
// 删除文章
const delBtn = async (id: number) => {
  await ElMessageBox.confirm('你确定要删除该文章吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await delArticle(id)
  ElNotification({
    title: '温馨提示',
    message: '删除成功',
    type: 'success',
    duration: 1500
  })
  getArticle()
}

onMounted(() => {
  getArticle()
})
</script>

<template>
  <page-container title="文章管理">
    <!-- 表单 -->
    <el-form style="display: flex">
      <el-form-item label="关键词：">
        <el-input
          v-model="params.key"
          placeholder="请输入关键词"
          style="width: 240px; margin-right: 20px"
        />
      </el-form-item>
      <el-form-item label="审核状态：">
        <el-select
          v-model="params.status"
          placeholder="请选择"
          style="width: 240px; margin-right: 20px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button type="primary" @click="searchBtn">搜索</el-button>
        <el-button @click="resetBtn">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="articleData"
      v-loading="loading"
      style="width: 100%"
      :row-style="{ height: '90px' }"
    >
      <el-table-column type="index" label="序号" width="100" align="center" />
      <el-table-column label="标题" align="center">
        <template #default="{ row }">
          <span class="text">{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center">
        <template #default="{ row }">
          <span class="text">{{ row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template #default="{ row }">
          <span v-if="row.status === 0">待审核</span>
          <span v-if="row.status === 1">审核通过</span>
          <span v-if="row.status === 2">审核未通过</span>
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="primary" link @click="selectDetail(row)"
            >查看</el-button
          >
          <template v-if="row.status !== 0">
            <el-button type="primary" link @click="delBtn(row.id)"
              >删除</el-button
            >
          </template>
          <template v-if="row.status === 0">
            <el-button type="primary" link @click="btnCheckClose(row, 1)"
              >通过</el-button
            >
            <el-button type="primary" link @click="btnCheckClose(row, 2)"
              >驳回</el-button
            >
          </template>
        </template>
      </el-table-column>
      <!-- 暂无数据 -->
      <template #empty>
        <el-empty description="暂无数据" />
      </template>
    </el-table>

    <!-- 分页 -->
    <el-row
      style="display: flex; justify-content: end"
      v-if="articleData.length > 0"
    >
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.pageSize"
        :page-sizes="[2, 3, 5, 10]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px"
      />
    </el-row>
  </page-container>

  <!-- 抽屉 - 文章 -->
  <el-drawer v-model="isShow" title="文章详情" direction="rtl" size="36%">
    <el-form :model="articleDetail" label-width="50px" style="padding: 10px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="articleDetail.title" disabled></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input
          v-model="articleDetail.content"
          disabled
          type="textarea"
          autosize
        ></el-input>
      </el-form-item>
      <el-form-item label="配图" v-if="articleDetail.image.length > 0">
        <el-carousel :interval="3000" height="300px" style="width: 100%">
          <el-carousel-item
            v-for="(item, index) in articleDetail.image"
            :key="item"
            style="display: flex; justify-content: center; align-items: center"
            @click="showImgViewer(index)"
          >
            <el-image :src="item" class="img" />
          </el-carousel-item>
        </el-carousel>
        <el-image-viewer
          v-if="isShowImg"
          :url-list="articleDetail.image"
          :initial-index="currentIndex"
          hide-on-click-modal
          @close="isShowImg = false"
        />
      </el-form-item>
      <el-form-item>
        <template v-if="articleDetail.status === 0">
          <el-button type="primary" @click="btnOK(1)">通过</el-button>
          <el-button @click="btnOK(2)">驳回</el-button>
        </template>
        <template v-if="articleDetail.status === 1">
          <el-button type="primary" disabled>已通过</el-button>
        </template>
        <template v-if="articleDetail.status === 2">
          <el-button disabled>已驳回</el-button>
        </template>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<style scoped lang="scss">
.text {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.img {
  height: 100%;
  border-radius: 8px;
}
</style>
